package com.jt.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAllUsers() {

        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String pwd = user.getPassword();
        String md5Str = DigestUtils.md5DigestAsHex(pwd.getBytes());
        user.setPassword(md5Str);
        QueryWrapper<User> queryWrapper = new QueryWrapper(user);
        User userDB = userMapper.selectOne(queryWrapper);
        String uuid = UUID.randomUUID().toString().replace("-","");

        return userDB==null?null:uuid;
    }

    /**
     * Mysql的分页语法: 1.0版本
     *  select * from user limit 起始位置0,查询的条数
     *  Sql: 1页2条   n页数/m条数
     *      SELECT * FROM  USER LIMIT 0,2   第一页
     *      SELECT * FROM  USER LIMIT 2,2   第二页
     *      SELECT * FROM  USER LIMIT 4,2   第三页
     *      SELECT * FROM  USER LIMIT 6,2   第四页
     *      SELECT * FROM  USER LIMIT (n-1)m,m   第N页
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getUserList2(PageResult pageResult) {//3
        //1.获取总记录数
        long total = userMapper.selectCount(null);
        //2.获取分页后的结果
        int pageNum = pageResult.getPageNum();  //页数
        int pageSize = pageResult.getPageSize();   //行数
        int startIndex = (pageNum-1) * pageSize;   //起始位置
        List<User> rows = userMapper.getUserList(startIndex,pageSize);
        //将数据进行封装
        pageResult.setTotal(total).setRows(rows); //+2个参数
        return pageResult;  //最终5个参数返回
    }

    /**
     * 利用MP的方式实现分页查询
     * API说明： 是selectPage(arg1,arg2)
     * arg1: MP中的分页对象 固定的
     * arg2: MP分页查询条件构造器
     *
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getUserList(PageResult pageResult) {
        IPage iPage = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.eq(flag,"username", pageResult.getQuery());
        iPage = userMapper.selectPage(iPage,queryWrapper);
        long total = iPage.getTotal();
        List<User> rows = iPage.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    //user:{id:xxx,status:true/false}
    @Override
    @Transactional
    public void updateStatus(User user) {

        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void addUser(User user) {
        String pwd = user.getPassword();
        String md5Str = DigestUtils.md5DigestAsHex(pwd.getBytes());
        user.setPassword(md5Str).setStatus(true);
        userMapper.insert(user);
    }
    @Override
    @Transactional
    public void deleteUser(Integer id) {

        userMapper.deleteById(id);
    }

    @Override
    public User findUserById(Integer id) {

        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {

        userMapper.updateById(user);
    }


    /**
     * Mysql的分页语法: 1.0版本
     *  select * from user limit 起始位置0,查询的条数
     *  Sql: 1页2条   n页数/m条数
     *      SELECT * FROM  USER LIMIT 0,2   第一页
     *      SELECT * FROM  USER LIMIT 2,2   第二页
     *      SELECT * FROM  USER LIMIT 4,2   第三页
     *      SELECT * FROM  USER LIMIT 6,2   第四页
     *      SELECT * FROM  USER LIMIT (n-1)m,m   第N页
     * @param pageResult
     * @return
     */
   /* @Override
    public PageResult getUserList(PageResult pageResult) {//3
        //1.获取总记录数
        long total = userMapper.selectCount(null);
        //2.获取分页后的结果
        int pageNum = pageResult.getPageNum();  //页数
        int pageSize = pageResult.getPageSize();   //行数
        int startIndex = (pageNum-1) * pageSize;   //起始位置
        List<User> rows = userMapper.getUserList(startIndex,pageSize);
        //List<User> rows = userMapper.getUserList2(pageResult); //利用对象查询
        //将数据进行封装
        pageResult.setTotal(total).setRows(rows); //+2个参数
        return pageResult;  //最终5个参数返回
    }*/

}
